const { ccclass, property } = cc._decorator;


@ccclass
export default class TimeUtils {
    static getTimeInMilliseconds() {
        return +new Date();
    };

    static getTimeInSeconds() {
        return Math.floor(+new Date() / 1000);
    };

    static getTimestamp() {
        return new Date().getTime();
    };

    static getDate() {
        return new Date().toLocaleDateString();
    };

    static getTargetTimestamp(hours = 0, minutes = 0, seconds = 0) {
        const currentDate = new Date(new Date().toLocaleDateString()).getTime();
        return new Date(currentDate + (1000 * (3600 * hours + 60 * minutes + seconds))).getTime();
    };

    static msToHMS(milliseconds, separator = ":", showHour = true) {
        let hours = Math.floor(milliseconds / 3600000);
        let minutes = Math.floor((milliseconds - hours * 3600000) / 60000);
        let seconds = Math.floor((milliseconds - hours * 3600000 - minutes * 60000) / 1000);

        if (showHour || hours !== 0) {
            hours = hours.toString().padStart(2, "0") + separator;
        } else {
            hours = "";
        }

        minutes = minutes.toString().padStart(2, "0");
        seconds = seconds.toString().padStart(2, "0");

        return hours + minutes + separator + seconds;
    };

    static secondsToHMS(seconds, showHour = true) {
        if (seconds < 60 && showHour) {
            return "00:00:" + (seconds < 10 ? "0" + seconds : seconds);
        }

        if (seconds < 60 && !showHour) {
            return "00:" + (seconds < 10 ? "0" + seconds : seconds);
        }

        if (!showHour && seconds < 3600) {
            const minutes = Math.floor(seconds / 60);
            seconds = seconds % 60;
            return (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds);
        }

        if (seconds < 3600) {
            return "00:" + (Math.floor(seconds / 60) < 10 ? "0" + Math.floor(seconds / 60) : Math.floor(seconds / 60)) + ":" + (seconds % 60 < 10 ? "0" + (seconds % 60) : seconds % 60);
        }

        const hours = Math.floor(seconds / 3600);
        const minutes = Math.floor(seconds % 3600 / 60);
        seconds = seconds % 60;

        return (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds);
    };

    static getUTCTime() {
        const currentDate = new Date();
        const utcTime = new Date(currentDate.getUTCFullYear(), currentDate.getUTCMonth(), currentDate.getUTCDate(),
            currentDate.getUTCHours(), currentDate.getUTCMinutes(), currentDate.getUTCSeconds()).getTime();
        return Math.floor(utcTime / 1000);
    };

    static formatSeconds(seconds) {
        let time = Math.floor(seconds);
        let days = 0;
        let hours = 0;
        let minutes = 0;

        if (time > 60) {
            minutes = Math.floor(time / 60);
            time = Math.floor(time % 60);

            if (minutes > 60) {
                hours = Math.floor(minutes / 60);
                minutes = Math.floor(minutes % 60);

                if (hours > 24) {
                    days = Math.floor(hours / 24);
                    hours = Math.floor(hours % 24);
                }
            }
        }

        let formattedTime = "";

        if (time > 0) {
            formattedTime = Math.floor(time) + "秒";
        }

        if (minutes > 0) {
            formattedTime = Math.floor(minutes) + "分" + formattedTime;
        }

        if (hours > 0) {
            formattedTime = Math.floor(hours) + "小时" + formattedTime;
        }

        if (days > 0) {
            formattedTime = Math.floor(days) + "天" + formattedTime;
        }

        return formattedTime;
    };
}
